﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRMBussines
{
    public partial class CrmBussines : IDisposable
    {
        public List<Sectore> ObtenSectores()
        {
            List<Sectore> res = new List<Sectore>();

            db = new CRMModelControladorDataContext(_cadenaConexion);
            var zns = (from zon in db.Sectores select zon);
            res = zns.ToList<Sectore>();

            return res;
        }

        public Sectore ObtenSector(int id)
        {
            Sectore de = new Sectore();

            db = new CRMModelControladorDataContext(_cadenaConexion);
            de = db.Sectores.Single(c => c.SectoresId == id);

            return de;
        }

        public void GuardaSector(Sectore sector)
        {
            db = new CRMModelControladorDataContext(_cadenaConexion);
            db.Sectores.InsertOnSubmit(sector);
            db.SubmitChanges();
        }

        public void ActualizaSector(Sectore sector)
        {
            Sectore sec = new Sectore();
            db = new CRMModelControladorDataContext(_cadenaConexion);
            sec = db.Sectores.Single(c => c.SectoresId == sector.SectoresId);
            sec.Descripcion = sector.Descripcion;

            db.SubmitChanges();
        }

        public void EliminaSector(int id)
        {
            Sectore sec = new Sectore();

            db = new CRMModelControladorDataContext(_cadenaConexion);
            sec = db.Sectores.Single(c => c.SectoresId == id);
            db.Sectores.DeleteOnSubmit(sec);
            db.SubmitChanges();
        }

        public List<SubSectore> ObtenSubSectoress()
        {
            List<SubSectore> res = new List<SubSectore>();

            db = new CRMModelControladorDataContext(_cadenaConexion);
            var zns = (from zon in db.SubSectores select zon);
            res = zns.ToList<SubSectore>();

            return res;
        }

        public IQueryable<resultadoSubSector> ObtenSubSectores(int id)
        {


            db = new CRMModelControladorDataContext(_cadenaConexion);

            IQueryable<resultadoSubSector> res = (from x in db.SubSectores
                                                      where x.SectoresId == id
                                                  select new resultadoSubSector()
                                                      {
                                                          SubSectoresId = x.SubSectoresId,
                                                          SectoresId = x.SectoresId,
                                                          Descripcion = x.Descripcion,
                                                          Sector = x.Sectore.Descripcion
                                                      }
                                            );


            return res;
        }

        public SubSectore ObtenSubSector(int id)
        {
            SubSectore de = new SubSectore();

            db = new CRMModelControladorDataContext(_cadenaConexion);
            de = db.SubSectores.Single(c => c.SubSectoresId == id);

            return de;
        }

        public void GuardaSubSector(SubSectore subsector)
        {
            db = new CRMModelControladorDataContext(_cadenaConexion);
            db.SubSectores.InsertOnSubmit(subsector);
            db.SubmitChanges();
        }

        public void ActualizaSubSector(SubSectore subsector)
        {
            SubSectore subs = new SubSectore();
            db = new CRMModelControladorDataContext(_cadenaConexion);
            subs = db.SubSectores.Single(c => c.SubSectoresId == subsector.SubSectoresId);
            subs.Descripcion = subsector.Descripcion;
            db.SubmitChanges();
        }

        public void EliminaSubSector(int id)
        {
            SubSectore subs = new SubSectore();

            db = new CRMModelControladorDataContext(_cadenaConexion);
            subs = db.SubSectores.Single(c => c.SubSectoresId == id);
            db.SubSectores.DeleteOnSubmit(subs);
            db.SubmitChanges();
        }   
    }

    public class resultadoSubSector
    {
        public int SubSectoresId { get; set; }
        public int SectoresId { get; set; }
        public string Descripcion { get; set; }
        public string Sector { get; set; }
    }

}
